package com.qupai.controller.user;

import com.qupai.dto.OrdersPaymentDTO;
import com.qupai.dto.OrdersSubmitDTO;
import com.qupai.result.PageResult;
import com.qupai.result.Result;
import com.qupai.service.OrderService;
import com.qupai.vo.OrderPaymentVO;
import com.qupai.vo.OrderSubmitVO;
import com.qupai.vo.OrderVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Tag(name = "志愿-C端-订单相关接口")
@Slf4j
public class OrderController {
    @Resource
    private OrderService orderService;

    @PostMapping("/submit")
    @Operation(summary = "用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单，参数为:{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    @PutMapping("/payment")
    @Operation(summary = "订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("订单支付结果：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     *
     * @param page     第几页（页码）
     * @param pageSize 每页几条数据
     * @param status   订单状态 1待付款 2待接单 3已接单 4已完成 5已取消
     */
    @GetMapping("/historyOrders")
    @Operation(summary = "历史订单查询")
    public Result<PageResult> page(Long page, Long pageSize, Integer status) {
        PageResult pageResult = orderService.pageQueryUser(page, pageSize, status);
        return Result.success(pageResult);
    }

    @GetMapping("/orderDetail/{id}")
    @Operation(summary = "查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }

    @PutMapping("/cancel/{id}")
    @Operation(summary = "取消订单")
    public Result<String> cancel(@PathVariable("id") Long id) {
        orderService.userCancelById(id);
        return Result.success();
    }

    @PostMapping("/repetition/{id}")
    @Operation(summary = "再来一单")
    public Result<String> repetition(@PathVariable Long id) {
        orderService.repetition(id);
        return Result.success();
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "根据订单ids集合删除订单")
    public Result<String> deleteByIds(@RequestBody List<Long> ids) {
        orderService.removeBatchByIds(ids);
        return Result.success();
    }

    @Operation(summary = "根据订单号集合删除订单")
    @DeleteMapping("/deleteByOrderNumbers")
    public Result<String> deleteByOrderNumbers(@RequestBody List<String> orderNumbers) {
        orderService.deleteByOrderNumbers(orderNumbers);
        return Result.success();
    }

}
